import {
  getFxdttj,
} from '../../../../api/statistics'
import * as echarts from '../../../../components/ec-canvas/echarts';
import nanjing from './nanjing'
echarts.registerMap("南京", nanjing)
let chart = null;
var option = {
  tooltip: {
    trigger: 'item'
  },
  visualMap: {
    min: 0,
    max: 1000,
    text: ['高', '低'],
    realtime: false,
    calculable: true,
    inRange: {
      color: ['lightskyblue', 'yellow', 'orangered']
    }
  },
  series: [{
    name: '区（隐患数量）',
    type: 'map',
    mapType: '南京',
    roam: true,
    itemStyle: {
      borderColor: '#000',
      borderWidth: 1,
      areaColor: 'lightskyblue'
    },
    label:{
      show:true,
      fontSize:8,
      color:'#000'
    },
    data: []
  }]
};

function initChart(canvas, width, height, dpr) {
  chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);
  return chart;
}
Component({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    startDate: '',
    endDate: '',
    ec: {
      onInit: initChart
    },
    form: {
      hy: '',
      zwbj: '',
      qh: '',
      startDate: '',
      endDate: ''
    }
  },
  methods: {
    // 时间选择器返回数据
    dateChange(e) {
      this.setData({
        'form.startDate': e.detail.startDate,
        'form.endDate': e.detail.endDate
      })
    },
    // 区选择器返回数据

    quChange(event) {
      this.setData({
        'form.qh': event.detail.bm,
      })
    },
    // 主委办局返回数据
    zwbjChange(event) {
      this.setData({
        'form.zwbj': event.detail.bm
      })
    },
    // 行业返回数据
    sshyChange(event) {
      this.setData({
        'form.hy': event.detail
      })
    },
    clear() {
      this.setData({
        form: {
          hy: '',
          zwbj: '',
          qh: '',
          startDate: '',
          endDate: ''
        }
      })
      this.initData()
    },
    initData() {
      this.setData({
        loading: true
      })
      getFxdttj({
        startdate: this.data.form.startDate,
        enddate: this.data.form.endDate,
        hy: this.data.form.hy,
      }).then(res => {
        let seriesData = []
        let maxNum = 0
        res.data.forEach(item => {
          if (item.num > maxNum) {
            maxNum = item.num
          }
          seriesData.push({
            name: item.mch,
            value: Number(item.num),
          })
        })
        option.series[0].data = seriesData;
        option.visualMap.max = Number(maxNum)+10;
        // 给chart图表添加接口返回的数据
        setTimeout(() => {
          chart.setOption(option);
          this.setData({
            loading: false
          })
        }, 2000);
      })
    },
  },
  ready() {
    this.initData()
  }
})